package com.itheima.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetMealDao;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    SetMealDao setmealDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    JedisPool jedisPool;

    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> setmealNames = new ArrayList<>();
        List<Map<String, Object>> setmealCount = new ArrayList<>();
        List<Map<String, Object>> setmeals = orderDao.getOrderCountBySetmeal();
        for (Map<String, Object> customSetmeal : setmeals) {
            Map<String, Object> count = new HashMap<>();
            Integer setmealId = Integer.parseInt((String) customSetmeal.get("setmeal_id"));
            Integer setmenlCount = Integer.parseInt((String) customSetmeal.get("setmenlCount"));
            Setmeal setmeal = setmealDao.findById(setmealId);
            count.put("name", setmeal.getName());
            count.put("value", setmenlCount);
            setmealNames.add(setmeal.getName());
            setmealCount.add(count);
        }
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);
        return map;
    }

    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
        //获得当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //查看今天新增会员数
        int todayCount = memberDao.findCountMemberByToday(today);
        //今日预约数
        int todayOrderNumber = orderDao.findOrderCountByDate(today);
        //今日到诊数
        int todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //json转换对象
        ObjectMapper json = new ObjectMapper();
        String todayCountInRedis = jedisPool.getResource().get("todayCount");
        String todayOrderNumberInRedis = jedisPool.getResource().get("todayOrderNumber");
        String todayVisitsNumberInRedis = jedisPool.getResource().get("todayVisitsNumber");
        if (todayCountInRedis!=null&&todayOrderNumberInRedis!=null&&todayVisitsNumberInRedis!=null){
            if (todayCount==Integer.parseInt(todayCountInRedis)&&
                todayOrderNumber==Integer.parseInt(todayOrderNumberInRedis)&&
                todayVisitsNumber==Integer.parseInt(todayVisitsNumberInRedis)){
                String report = jedisPool.getResource().get("businessReport");
                HashMap<String,Object> result = json.readValue(report, HashMap.class);
                return result;
            }
        }
        //查看总会员数
        int countAll = memberDao.findCountAll();
        //查看本周新增会员数
        int thisWeekCount = memberDao.findThisWeekCount(thisWeekMonday);
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);
        //热门套餐（取前4）
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayCount);
        result.put("totalMember",countAll);
        result.put("thisWeekNewMember",thisWeekCount);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        String string = json.writeValueAsString(result);
        jedisPool.getResource().set("todayCount",todayCount+"");
        jedisPool.getResource().set("todayOrderNumber",todayOrderNumber+"");
        jedisPool.getResource().set("todayVisitsNumber",todayVisitsNumber+"");
        jedisPool.getResource().set("businessReport",string);
        return result;
    }

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception{
        //获得当前系统时间
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //动态获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //动态获得本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);//select count(id) from t_member where regTime = today
        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //json转换对象
        ObjectMapper json = new ObjectMapper();
        String todayCountInRedis = jedisPool.getResource().get("todayCount");
        String todayOrderNumberInRedis = jedisPool.getResource().get("todayOrderNumber");
        String todayVisitsNumberInRedis = jedisPool.getResource().get("todayVisitsNumber");
        if (todayCountInRedis!=null&&todayOrderNumberInRedis!=null&&todayVisitsNumberInRedis!=null){
            if (todayNewMember==Integer.parseInt(todayCountInRedis)&&
                    todayOrderNumber==Integer.parseInt(todayOrderNumberInRedis)&&
                    todayVisitsNumber==Integer.parseInt(todayVisitsNumberInRedis)){
                String report = jedisPool.getResource().get("businessReport");
                HashMap<String,Object> result = json.readValue(report, HashMap.class);
                return result;
            }
        }
        //总会员数据
        Integer totalMember = memberDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);



        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        String string = json.writeValueAsString(result);
        return result;
    }
}
